package com.hoodbrains.insideloc.maps.UI;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import com.hoodbrains.insideloc.maps.*;

public class CreationView extends DrawingView {

	private Gondole gondoleID; // variable to know what ball is being dragged

	public CreationView(Context context, AttributeSet attrs) {// ,Magasin
																// magasin) {
		super(context, attrs);
		setFocusable(true);
		//this.supermarket = new Magasin();
	}

	public void incrementZoom()
	{
		mScaleFactor -= 0.25f ;
		
		
	}
	
	
	
	public void decrementZoom()
	{
		mScaleFactor+= 0.25f;
	}
	
	// events when touching the screen
	public boolean onTouchEvent(MotionEvent event) {
		int eventaction = event.getAction();

		int X = (int) ((event.getX() - getWidth() / 2 - mPosX) / mScaleFactor);
		int Y = (int) ((-event.getY() + getHeight() / 2 + mPosY) / mScaleFactor);
		// marchait sans sle zoom
		// int X = (int)(event.getX()/mScaleFactor-getWidth()/2 - mPosX) ;
		// int Y = (int)(-event.getY()/mScaleFactor + getHeight()/2 + mPosY) ;
		Log.e("DEBUG VIEW", "Event : " + X + "," + Y);
		switch (eventaction) {

		case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on
										// a ball
			if (gondoleID!=null){
				gondoleID.setSelected(false);
			}
			this.gondoleID = null;
			if (this.supermarket.getGondoles() != null) {
				for (Gondole cur : this.supermarket.getGondoles()) {
					Rect gondRect = new Rect(cur.getX(), cur.getY(), cur.getX()
							+ cur.getWidth(), cur.getY() + cur.getHeight());
					Log.e("DEBUG VIEW", cur.getX() + "," + cur.getY() + " : "
							+ cur.getX() + " " + cur.getWidth() + ","
							+ cur.getY() + " " + cur.getHeight());
					Log.e("DEBUG VIEW", "Event : " + X + "," + Y);
					if (gondRect.contains(X, Y)) {
						this.gondoleID = cur;
						this.gondoleID.setSelected(true);

					}
					if (gondoleID == null) {
						super.onTouchEvent(event);
					}

				}
			}

			// Log.d("Debug View", "Touching !"+this.gondoleID.getX());

			break;

		case MotionEvent.ACTION_MOVE: // touch drag with the ball
			// move the balls the same as the finger
			if (this.gondoleID != null) {
				this.gondoleID.setX(X - 25);
				this.gondoleID.setY(Y - 25);
			} else {
				super.onTouchEvent(event);
			}

			break;

		case MotionEvent.ACTION_UP:
			// touch drop - just do things here after dropping
			// if (this.gondoleID == null) {
			// super.onTouchEvent(event);
			// }
			break;
		}
		// redraw the canvas
		invalidate();
		return true;

	}
	public Gondole getSelectedGondole(){
		return gondoleID ;
	}
	
}
